package test00.tx05;

import java.util.Arrays;
import java.util.Comparator;
import java.util.function.Predicate;

public class MainTest01 {
    public static void main(String[] args) {
        //五、JDK函数式接口的使用
        //1.定义一个学生类（姓名、年龄）,使用Supplier接口，
        Student[] students = {
                new Student("张说三", 20),
                new Student("李四", 22),
                new Student("王是五", 17),
                new Student("李五", 19),
                // 可继续添加更多学生
        };
        Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                return s2.age - s1.age;
            }
        });
        //打印输出年龄最小的学生，格式为：姓名-年龄
        Supplier<Student> supplier = ()->{
            Arrays.sort(students, Comparator.comparingInt(Student::getAge)); // 补全方法引用
            return students[0];
        };
        System.out.println(supplier.get());
        //
        //2.定义一个方法：实现判断一个分数是否合格，并打印输出结果。
        int score = 59;

        Predicate<Integer> predicate = x -> x >= 60;

        System.out.println(predicate.test(score));
        //
        //3.定义一个学生类（姓名、年龄）；定义一个数组，存储若干学生；
        //定义三个Consumer，分别用于打印输出姓李的学生信息、
        //名字有三个字的学生信息、年龄大于18岁的学生。
        Consumer consumer01 = (stu)->{
            for (Student student:stu){
                if (student.name.startsWith("李")) {
                    System.out.println(student.name + "-" + student.age);
                }
            }
        };
        consumer01.Consumer(students);
        Consumer consumer02 = (stu)->{
            for (Student student:stu){
                if (student.name.length()==3) {
                    System.out.println(student.name + "-" + student.age);
                }
            }
        };
        consumer02.Consumer(students);
        Consumer consumer03 = (stu)->{
            for (Student student:stu){
                if (student.age >= 18) {
                    System.out.println(student.name + "-" + student.age);
                }
            }
        };
        consumer03.Consumer(students);
        //
        //4.判断邮箱地址是否合法，帐号必须至少由5个字符组成，而且有且只有一个@字符
        String eamils = "ewq@q";
        Eamil eamil = (s)->{
            char[] chars = s.toCharArray();
            if (chars.length >= 5) {
                int num = 0;
                for (char c : chars) {
                    if (c == '@')num ++;
                    if (num>1) {
                        System.out.println("@大于二");
                        return;
                    }
                }
                System.out.println(chars);
            }
        };
        eamil.e(eamils);
        //
        //5.把字符串"张三,18"转换为学生对象（学生类同上）
        String n = "张三,18";
        String name = n.split(",")[0];
        int age = Integer.parseInt(n.split(",")[1]);
        Student students1 = new Student(name,age);
        System.out.println(students1.name+"-"+students1.age);
        //
        //6.把字符串"张三,18"转换为学生对象，获取学生对象的年龄，
        //加1岁后转换为整数。
    }
}
